package com.erp.service.util.net;

import java.io.File;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.http.entity.FileEntity;
/**
 * 
 * @author ccl
 * 自定义支持传出进度的上传文件实体
 */
public class NdFileEntity extends FileEntity {
	private final UploadProgressListener listener;
	private CountingOutputStream outputStream;
	private OutputStream lastOutputStream;
	private long transferred = 0;
	private OutputStream wrappedOutputStream;
	private File file;

	private long startPosition=0;
	/**
	 * 构造函数
	 * @param file 要上传的文件
	 * @param contentType ContentType类型
	 * @throws IOException
	 */
	public NdFileEntity(File file, String contentType) throws IOException {
		this(file, contentType,0, null);
	}

	/**
	 * 构造函数
	 * @param file 要上传的文件
	 * @param contentType ContentType类型
	 * @param startPosition 开始位置
	 * @param listener 上传进度监听器
	 * @throws IOException 
	 */
	public NdFileEntity(File file, String contentType,long startPosition, final UploadProgressListener listener) throws IOException {
		super(file, contentType);
		this.file = file;
		this.listener = listener;
		this.startPosition=startPosition;
		
	}
	
//	@Override
//	public InputStream getContent() throws IOException {
//		InputStream inputStream= super.getContent();
//		
//		RandomAccessFile fos =new RandomAccessFile(erp_com_file,"r");
//		fos.seek(startPosition);
//		fos.
//		
//		BufferedInputStream bis =  bis = new BufferedInputStream(con.getInputStream()) ;
////		inputStream
//		return inputStream;
//	};

	/**
	 * 写入输出流
	 */
	@Override
	public void writeTo(OutputStream outstream) throws IOException {
		super.writeTo(new CountingOutputStream(outstream, this.listener,this.file,this.startPosition));
	}

	/**
	 * 统计输出流
	 * @author ccl
	 *
	 */
	public static class CountingOutputStream extends FilterOutputStream {

		private final UploadProgressListener listener;
		private long transferred;
		private long startPosition;
		private File file;
		
		/**
		 * 构造函数
		 * @param out 输出流
		 * @param listener 上传进度监听器
		 * @param file 上传的文件
		 * @param startPosition 开始位置
		 */
		public CountingOutputStream(final OutputStream out, final UploadProgressListener listener,File file,long startPosition) {
			super(out);
			this.listener = listener;
			this.transferred = 0;
			this.startPosition=startPosition;
			this.file=file;
		}

		/**
		 * 写入字节流
		 * @param b 字节流
		 * @param off 偏移量
		 * @param len 长度
		 */
		public void write(byte[] b, int off, int len) throws IOException {
//			if(transferred+len>this.startPosition){
//				int tempLen=(int)(transferred-startPosition);
//				if(tempLen<len){
//					len=tempLen;
//					off=off+(len-tempLen);
//				}
//				out.write(b, off, len);
//			}
			out.write(b, off, len);
			this.transferred += len;
			
			if (listener != null)
				this.listener.transferred(this.transferred,this.file);
		}
		/**
		 * 写入字节流
		 */
		public void write(int b) throws IOException {
			out.write(b);
			this.transferred++;
			if (listener != null)
				this.listener.transferred(this.transferred,this.file);
		}
	}
}
